$(function(){
    $("body").on('click','[stopPropagation]',function (e) {e.stopPropagation();});
    init_person_data();
});

function init_person_data(){

    $("#selectTaskTime span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#selectTaskTime').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        minDate: moment().add('year',-1), //设置最小日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#selectTaskTime span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $("#lianxiTime").val("最近联系时间:自定义");
        $JuXue.refreshTable("phonePersonTaskTable");
    });

    /**初始化列表**/
    var settings = {
        url: ctx + "SalePhone/getAllCphoneTaskMyByBatch",
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                //筛选条件
                spUuid:   $("#selectUUids").val(),//批次id
                spRecer:   $("#cExZiSource").val(), //分配的人
                starttime :$("#starttime").val(),     //开始时间
                endtime :  $("#endtime").val(),         //结束时间
                spStatus: $("#noticeStatus").val()     //状态
            };
        },
        columns: [{
            field: 'spPhone',
            title: '联系电话',
            class:'text-center'
        }, {
            field: 'spName',
            title: '姓名'
        }, {
            field: 'spLastRecord',
            title: '最近联系时间'
        }, {
            field: 'spRecordUrl',
            title: '通话录音',
            formatter :function (val,row,index) {
                return "<audio class='height-25 width-200' src="+val+" controlsList=\"nodownload\" controls=\"controls\" type=\"audio/wav\"></audio>";
            }
        }, {
            field: 'spStatus',
            title: '通话标记',
            formatter :function (value,row,index) {
                var uid = row.spId;
                var seclect='';
                if(value=="8"){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                            "<option  value='1'>空号</option><option value='2'>未接听</option><option value='3'>关机</option><option value='4'>停机</option>" +
                            "<option value='5'>有意向</option><option value='6'>无意向</option><option value='8' selected>在考虑</option>" +
                        "</select>";
                }else if(value=='1'){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option  value='1' selected >空号</option><option value='2'>未接听</option><option value='3'>关机</option><option value='4'>停机</option>" +
                        "<option value='5'>有意向</option><option value='6'>无意向</option><option value='8'>在考虑</option>" +
                        "</select>";
                }else if(value=='2'){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option  value='1' selected >空号</option><option value='2' selected>未接听</option><option value='3'>关机</option><option value='4'>停机</option>" +
                        "<option value='5'>有意向</option><option value='6'>无意向</option><option value='8'>在考虑</option>" +
                        "</select>";
                }else if(value=='3'){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option  value='1' selected >空号</option><option value='2'>未接听</option><option value='3' selected>关机</option><option value='4'>停机</option>" +
                        "<option value='5'>有意向</option><option value='6'>无意向</option><option value='8'>在考虑</option>" +
                        "</select>";
                }else if(value=='4'){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option  value='1' selected >空号</option><option value='2'>未接听</option><option value='3'>关机</option><option value='4' selected>停机</option>" +
                        "<option value='5'>有意向</option><option value='6'>无意向</option><option value='8'>在考虑</option>" +
                        "</select>";
                }else if(value=='5'){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option  value='1' selected >空号</option><option value='2'>未接听</option><option value='3'>关机</option><option value='4'>停机</option>" +
                        "<option value='5' selected>有意向</option><option value='6'>无意向</option><option value='8'>在考虑</option>" +
                        "</select>";
                }else if(value=='6'){
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option  value='1' selected >空号</option><option value='2'>未接听</option><option value='3'>关机</option><option value='4'>停机</option>" +
                        "<option value='5'>有意向</option><option value='6'selected >无意向</option><option value='8'>在考虑</option>" +
                        "</select>";
                }else {
                    seclect="<select  class='form-control p-3 height-25' onchange='selectspStatus(this)' data='"+uid+"'>" +
                        "<option >请选择标记</option><option  value='1' selected >空号</option>" +
                        "<option value='2'>未接听</option><option value='3'>关机</option><option value='4'>停机</option>" +
                        "<option value='5'>有意向</option><option value='6'>无意向</option><option value='8' >在考虑</option>" +
                        "</select>";
                }
                return seclect;
            }
        }, {
            field: 'spFenName',
            title: '操作',
            formatter:function (value, row, index) {
                var uid = row.spId;
                var name = row.spName;
                var phone = row.spPhone;
                return " <button class='btn btn-xs btn-link' onclick='makePhoneForTask(this)' data-uid='"+uid+"' cname='"+name+"' phone='"+phone+"' >呼叫</button>\n" +
                    "<button class='btn btn-xs btn-link' onclick='addTomyTask(this);' cname='"+name+"' phone='"+phone+"'>添加为客户</button>";
            }
        }]
    };
    $JuXue.initTable('phonePersonTaskTable', settings);
}
/**确定时间**/
function submitPTimeTask(){
    $('#selecttimetask').removeClass('open');
    $("#select_Psale_time").find("button").each(function () {
        if($(this).hasClass("btn-primary")){
            $("#lianxiTime").val("最近联系时间:"+$(this).text());return false;
        }
    });
    $JuXue.refreshTable("phonePersonTaskTable");
}
/**选择人员**/
function selectspStatus(btn){
    var val = $(btn).val();
    var data = $(btn).attr("data");
    $JuXue.confirm({
        text: "确定删修改？",
        confirmButtonText: "确定修改"
    }, function () {
        $.post(ctx+"SalePhone/updateTaskPhoneRecord",{spId:data,spStatus:val},function (r) {
            if (r.code == 0){$JuXue.n_success("修改成功");}else{$JuXue.n_danger("修改失败")}
        });
    });
}

/**时间选择**/
function selectPTaskTime(btn){
    var data = $(btn).attr("data");
    if(data === "sel"){
        $("#selectTaskTime").removeClass("hidden");
        $("#starttime").val(moment().subtract("days",29).format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else if (data == "1") {
        //今天
        $("#selectTaskTime").addClass("hidden");
        $("#starttime").val(moment().format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else if (data == "2"){ //昨天
        $("#selectTaskTime").addClass("hidden");
        $("#starttime").val(moment().subtract(1, 'days').format("YYYY-MM-DD"));
        $("#endtime").val(moment().subtract(1, 'days').format("YYYY-MM-DD"));
    }else if (data == "7") {
        $("#selectTaskTime").addClass("hidden");
        $("#starttime").val(moment().subtract(6, 'days').format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else if (data == "30"){
        $("#selectTaskTime").addClass("hidden");
        $("#starttime").val(moment().startOf('month').format("YYYY-MM-DD"));
        $("#endtime").val(moment().endOf('month').format("YYYY-MM-DD"));
    }else{
        $("#selectTaskTime").addClass("hidden");
        $("#starttime").val("");
        $("#endtime").val("");
    }
    $("#select_Psale_time").find("button").each(function () {
        $(this).removeClass("btn-primary");
    });
    $(btn).addClass("btn-primary");
}
/**改变状态**/
function pNoticeStatus(btn){
    var data = $(btn).attr("data");
    $("#selectPStatus").find("button").each(function () {
        $(this).removeClass("btn-primary");
    });
    $(btn).addClass("btn-primary");
    $("#noticeStatus").val(data);
    $("#labelstatus").val("通话标记:"+$(btn).text());
    $JuXue.refreshTable("phonePersonTaskTable");
}

/**返回首页面**/
function comePersonback(){
    if($("#selecttask").length > 0){
        var ids = $("#selectType").val();
        if (ids == "1"){
            $("#selecttask").trigger("click");
        }else{
            $("#selectMytask").trigger("click");
        }
    }else{
        $("#selectMytask").trigger("click");
    }
}

/**弄打电话**/
function makePhoneForTask(btn){
    var uuid = $(btn).attr("data-uid");
    var cname  = $(btn).attr("cname");
    var phone = $(btn).attr("phone");
    $("#suatomename").val(cname);
    $("#dialcode").val(phone);

    $("#cus_phone_uid").val(uuid);
    $("#cus_uid_type").val("4");
    sessionStorage.setItem("typeDia","2");
    $("#phone-modal").modal('show').draggable();

    $('#minute_show').html('<s></s>00 分');
    $('#second_show').html('<s></s>00 秒');
    $("#dialcodeNumid").show();
}

/**增加为我的客户**/
function addTomyTask(btn){
    var cname  = $(btn).attr("cname");
    var phone = $(btn).attr("phone");
    $.post(ctx+"SalePhone/loadAddCustomer",{},function (r) {
        $("#subcenterModal").empty().append(r).modal('show');
        $("#myCusName").val(cname);$("#myCusMobile").val(phone);
    });
}